package com.mstore.action;

import java.util.List;
import java.util.Map;

import com.mstore.pojo.Account;
import com.mstore.pojo.Shop;
import com.mstore.service.ShopService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 企业分店信息操作
 */
public class ShopAction extends BaseAction {

	private static final long serialVersionUID = 8893338192645484638L;

	private ShopService shopService;

	private Shop shop;

	private String json;

	private List<Shop> shops;

	private Map<String, Object> session = ActionContext.getContext()
			.getSession();

	/**
	 * 添加分店
	 * 
	 * @return
	 */
	public String addStore() {
		String temp = "error";
		Account account = (Account) session.get("account");
		if (account != null) {
			this.shop.setCreateTime(getCreateTime());
			this.shop.setEaId(account.getId());
			shopService.addShop(shop);
			temp = "addStore";
		}
		return temp;
	}

	/**
	 * 删除分店
	 * 
	 * @return
	 */
	public String deleteStore() {
		this.shopService.deleteShop(shop);
		return "deleteStore";
	}

	/**
	 * 更新分店信息
	 * 
	 * @return
	 */
	public String updateStore() {
		shopService.updateShop(shop);
		return "updateStore";
	}

	/**
	 * 获取一条对应店铺信息
	 * 
	 * @return
	 */
	public String editStore() {
		String temp = "error";
		Shop shop = (Shop) shopService.getOneShop(this.shop);
		if (shop != null) {
			this.shop = shop;
			temp = "editStore";
		}
		return temp;
	}

	/**
	 * 查询全部分店
	 * 
	 * @return
	 */
	public String getAllStore() {
		String temp = "error";
		Account account = (Account) session.get("account");
		if (account != null) {
			Shop shop = new Shop();
			shop.setEaId(account.getId());
			shops = this.shopService.getAllShop(shop);
			if (shops.size() > 0) {
				temp = "getAllStore";
			}
			else {
				temp = "nullStore";
			}
		}
		return temp;
	}

	/**
	 * 
	 * @return
	 */
	public String getAllStoreAjax() {
		String temp = "error";
		Account account = (Account) session.get("account");
		if (account != null) {
			Shop shop = new Shop();
			shop.setEaId(account.getId());
			List<Shop> shops = this.shopService.getAllShop(shop);
			json = listTojson(shops);
			temp = "getAllStoreAjax";
		}
		return temp;
	}

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public List<Shop> getShops() {
		return shops;
	}

	public void setShops(List<Shop> shops) {
		this.shops = shops;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

}
